import { createRouter, createWebHashHistory } from "vue-router";

//登录界面导入
import LoginVue from '../views/Login.vue'
//主页界面导入
import HomeVue from '../views/Home/Home.vue'
//引入子路由模块
import FileContent from '../components/File/FileContent.vue'
import RecycleContent from '../components/recycle/RecycleContent.vue'
// 引入主页面
import MainContent from '../components/homemain/HomeMain.vue'
//引入分享页面
import ShareContent from '../components/share/share.vue'

//定义路由规则
const     routes = [
    {  path:'/login', component:LoginVue},
    {  path:'/', redirect:'/login'},
    {  path:'/home', component:HomeVue,
        children: [
            {  path:'/', redirect:'/home'},
            {  path:'/home', component:MainContent},   
            {  path:'/home/file', component:FileContent},
            {  path:'/home/share', component:ShareContent},
            {  path:'/home/recycle', component:RecycleContent}
        ]
    }
        
];
//全局前置守卫路径
const beforeEach = (to, from, next) => {
    // token 验证
    let token = localStorage.getItem('Access-Token');
    
    if (to.path === '/login' && !token) {
        // 允许访问登录页面
        next(); 
    } else if (to.path === '/login' && token) {
        // 如果已经登录，重定向到主页
        next('/home');
    } else {
        // 对于其他路径，检查 token 是否存在
        if (!token) {
            next('/login'); // 未登录则重定向到登录
        } else {
            next(); // 已登录，可以继续
        }
    }
};

//创建路由器
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
    //全局前置守卫
    beforeEach: beforeEach

});
//导出路由器
export default router;

